package com.yonyou.cloud.member.domainmodel.entities.coupon;

import com.yonyou.cloud.member.domainmodel.vo.CouponId;
import com.yonyou.cloud.member.domainmodel.vo.MemberId;
import com.yonyou.cloud.member.libs.util.DateTimeUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.time.LocalDateTime;

@ApiModel(description = "优惠券")
public abstract class Coupon {
    @ApiModelProperty(value = "优惠券ID", dataType = "java.lang.Long")
    private CouponId id;
    @ApiModelProperty(value = "会员ID", dataType = "java.lang.Long")
    private MemberId memberId;
    @ApiModelProperty(value = "优惠券名称")
    private String title;
    @ApiModelProperty(value = "优惠券有效时间")
    private LocalDateTime effectDate;
    @ApiModelProperty(value = "优惠券过期时间")
    private LocalDateTime dueDate;
    private LocalDateTime createTime;
    private CouponStatusEnum status;

    public Coupon() {
    }

    public Coupon(CouponId id, MemberId memberId, String title) {
        this.id = id;
        this.memberId = memberId;
        this.title = title;
        this.createTime = DateTimeUtils.fromTimestamp(id.ts());
        this.status = CouponStatusEnum.NEW;
    }

    public Coupon setEffectiveDate(LocalDateTime effectDate, Integer periodDays) {
        this.effectDate = effectDate;
        this.dueDate = effectDate.plusDays(periodDays);
        return this;
    }

    public boolean isAvailable() {
        LocalDateTime currentTime = LocalDateTime.now();
        return status == CouponStatusEnum.NEW && effectDate.isBefore(currentTime) && dueDate.isAfter(currentTime);
    }

    public CouponId getId() {
        return id;
    }

    public MemberId getMemberId() {
        return memberId;
    }

    public void setMemberId(MemberId memberId) {
        this.memberId = memberId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public LocalDateTime getEffectDate() {
        return effectDate;
    }

    public void setEffectDate(LocalDateTime effectDate) {
        this.effectDate = effectDate;
    }

    public LocalDateTime getDueDate() {
        return dueDate;
    }

    public void setDueDate(LocalDateTime dueDate) {
        this.dueDate = dueDate;
    }

    public CouponStatusEnum getStatus() {
        return status;
    }

    public void setStatus(CouponStatusEnum status) {
        this.status = status;
    }
}